package com.zyjc.city.model;

import com.zyjc.city.enums.ResultCodeEnum;
import lombok.Data;

/**
 * @author ckj
 * @date 2024年03月29日 15:40
 */
@Data
public class Result<T> {
    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    //私有的无参构造
    private Result() {
    }

    //静态方法，返回一个result示例，只设置实例的数据
    public static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T data, Integer code) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> build( String message,T body) {
        Result<T> result = build(body);
        result.setMessage(message);
        return result;
    }


    /**
     * 操作成功
     **/
    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS.code);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL.code);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return build(msg, data);
    }

    public static <T> Result<T> fail() {
        return Result.fail(null);
    }


}
